package com.wzdigit.wms.wms.service.task;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.api.BasicSimpleCodeRuleApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.wms.basic.domain.dtos.inventory.UpdateInventoryInputDto;
import com.wzdigit.wms.basic.domain.inventory.Goods;
import com.wzdigit.wms.basic.domain.inventory.GoodsHis;
import com.wzdigit.wms.basic.domain.inventory.GoodsLog;
import com.wzdigit.wms.basic.domain.inventory.GoodsVersion;
import com.wzdigit.wms.basic.domain.oqc.OqcInsp;
import com.wzdigit.wms.basic.domain.order.TrsnDtl;
import com.wzdigit.wms.basic.domain.order.TrsnDtlGoods;
import com.wzdigit.wms.basic.domain.order.TrsnHeader;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnDtlGoods;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.enums.*;
import com.wzdigit.wms.basic.mapper.core.inventory.GoodsHisMapper;
import com.wzdigit.wms.basic.mapper.core.inventory.GoodsLogMapper;
import com.wzdigit.wms.basic.mapper.core.inventory.GoodsMapper;
import com.wzdigit.wms.basic.mapper.core.inventory.GoodsVersionMapper;
import com.wzdigit.wms.basic.mapper.core.oqc.OqcInspMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import com.wzdigit.wms.basic.service.code.InventoryService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 翻牌自动出库Service
 * Created by zyf on 2021/9/10 12:44
 */
@Service
@Slf4j
public class AutoQmsGdnService {

    private static final String OQC_GDN="oqc_gdn";
    private static final String GDN_D98="GDN_D98";
    private static final String TRSN_ORDER = "TRSN_ORDER";

    @Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;
    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;

    @Autowired
    private OqcInspMapper oqcInspMapper;
    @Autowired
    private GdnHeaderMapper gdnHeaderMapper;
    @Autowired
    private GdnDtlMapper gdnDtlMapper;
    @Autowired
    private GdnDtlGoodsMapper gdnDtlGoodsMapper;
    @Autowired
    private GoodsLogMapper goodsLogMapper;
    @Autowired
    private GoodsHisMapper goodsHisMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private TrsnHeaderMapper trsnHeaderMapper;
    @Autowired
    private TrsnDtlMapper trsnDtlMapper;
    @Autowired
    private TrsnDtlGoodsMapper trsnDtlGoodsMapper;
    @Autowired
    private GoodsVersionMapper goodsVersionMapper;

    @Autowired
    private InventoryService inventoryService;


    @Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
    private BasicDataItemDetailApi dataItemDetailApi;
    @Reference(version = "${dubbo.consumers.basic.simple.code.rule.api:1.0.0}", check = false)
    private BasicSimpleCodeRuleApi basicSimpleCodeRuleApi;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void autoGdn(List<GoodsVersion> goodsVersionList, OqcInsp oqcInsp)  throws Exception {
        goodsVersionList=goodsVersionMapper.selectBySnList(goodsVersionList.stream().map(t -> t.getGoodsSn()).collect(Collectors.toList()));
        goodsVersionList=goodsVersionList.stream().filter(i -> !i.getQaStatus().equals(QaStatusEnum.OK.code)).collect(Collectors.toList());
        List<DataItemDetailDTO> testList = dataItemDetailApi.getDataItemDetailByCode(OQC_GDN);
        //散件/内销类型
        List<String> soTypeList = testList.stream().map(t -> t.getItemName()).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(soTypeList)){
            log.info("无需自动出库!");
            return;
        }
        //默认货主
        DataItemDetailDTO dataItem = dataItemDetailApi.getDataItemDetailByPCodeAndName(DEFAULT_OWNER, DEFAULT_OWNER_NAME);
        Integer ownerId = (dataItem == null || dataItem.getItemValue() == null ? 0 : Integer.parseInt(dataItem.getItemValue()));

        //出库单生成变量
        Date nowDate=new Date();
        BigDecimal dtlQty;
        BigDecimal trsnQty;

        //分组变量
        List<GoodsVersion> allGoodsList;
        List<GoodsVersion> headerGoodsList;
        List<GoodsVersion> dtlGoodsList;
        List<GoodsVersion> trsnHeaderList;
        List<GoodsVersion> trsnDtlList;
        Map<String, List<GoodsVersion>> gdnHeaderCollect;
        Map<String, List<GoodsVersion>> gdnDtlCollect;
        Map<String, List<GoodsVersion>> invGoodsCollect;
        Map<String, List<GoodsVersion>> trsnHeaderCollect;
        Map<String, List<GoodsVersion>> trsnDtlCollect;

        Map<String, List<GoodsVersion>> soCollect = goodsVersionList.stream().collect(Collectors.groupingBy(GoodsVersion::getSoNo));
        for (Map.Entry<String, List<GoodsVersion>> entry : soCollect.entrySet()) {
            String type = oqcInspMapper.selectTypeBySo(entry.getKey());

            if (soTypeList.contains(type)){
                allGoodsList=entry.getValue();
                log.info(entry.getKey()+"生成出库单开始");
                gdnHeaderCollect = allGoodsList.stream().collect(Collectors.groupingBy(GoodsVersion::getFactory));
                for (Map.Entry<String, List<GoodsVersion>> headerEntry : gdnHeaderCollect.entrySet()) {
                    //生成出库单头
                    GdnHeader gdnHeader=new GdnHeader();
                    headerGoodsList=headerEntry.getValue();
                    setGdnHeader(gdnHeader,ownerId,oqcInsp.getInspNo(),nowDate,headerEntry.getKey());
                    gdnHeaderMapper.insert(gdnHeader);
                    int gdnLine=1;
                    gdnDtlCollect=headerGoodsList.stream().collect(
                            Collectors.groupingBy(t -> t.getWhsId() + "#" +
                                    t.getSkuId() + "#" + t.getQaStatus()+"#"+t.getLotno(), Collectors.toList()));
                    for (Map.Entry<String, List<GoodsVersion>> dtlEntry : gdnDtlCollect.entrySet()) {
                        GdnDtl gdnDtl=new GdnDtl();
                        dtlGoodsList=dtlEntry.getValue();
                        dtlQty=dtlGoodsList.stream()
                                .map(GoodsVersion::getQty)
                                .reduce(BigDecimal.ZERO,BigDecimal::add);
                        setGdnDtl(gdnHeader,gdnDtl,gdnLine,dtlGoodsList.get(0),nowDate,dtlQty);
                        gdnDtlMapper.insert(gdnDtl);
                        //生成出库凭证
                        TrsnHeader trsnHeader=new TrsnHeader();
                        trsnHeader.setNoteNo(basicSimpleCodeRuleApi.GenerateCode(TRSN_ORDER));
                        trsnHeader.setNoteType(TrsnNoteTypeEnum.OUT_STORAGE.getCode());
                        trsnHeader.setOwner(gdnHeader.getOwner());
                        trsnHeader.setFactory(gdnHeader.getFactory());
                        trsnHeader.setWhsId(gdnDtl.getWhsId());
                        trsnHeader.setVendorId(gdnDtl.getVendor());
                        trsnHeader.setFmDoc(gdnHeader.getGdnNo());
                        trsnHeader.setCreateUser("oqc-system");
                        trsnHeader.setStatus(TrsnHeaderStatusEnum.CMP.code);
                        trsnHeader.setCreateTime(nowDate);
                        trsnHeaderMapper.insert(trsnHeader);
                        TrsnDtl trsnDtl=new TrsnDtl();
                        trsnDtl.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
                        trsnDtl.setNoteNo(trsnHeader.getNoteNo());
                        trsnDtl.setNoteLineNo("1");
                        trsnDtl.setFmDoc(gdnHeader.getGdnNo());
                        trsnDtl.setFmDocLine(gdnDtl.getGdnLineNo()+"");
                        trsnDtl.setInvUpdateFlag(TrsnDtlInvUpdateFlagEnum.OUT.code);
                        trsnDtl.setSkuId(gdnDtl.getSkuId());
                        trsnDtl.setQty(dtlQty);
                        trsnDtl.setUnit(gdnDtl.getUnit());
                        trsnDtl.setInvAttr(gdnDtl.getInvAttr());
                        trsnDtl.setQaStatus(gdnDtl.getQaStatus());
                        trsnDtl.setLotAttr1(gdnDtl.getPoNo());
                        trsnDtl.setLotAttr2(gdnDtl.getSoNo());
                        trsnDtl.setLotAttr3("/");
                        trsnDtl.setStatus(TrsnDtlStatusEnum.CMP.code);
                        trsnDtl.setCreateUser("oqc-system");
                        trsnDtl.setCreateTime(nowDate);
                        trsnDtlMapper.insert(trsnDtl);

                        for (GoodsVersion goodsVersion : dtlGoodsList) {
                            GdnDtlGoods gdnDtlGoods=new GdnDtlGoods();
                            setGdnDtlGoods(gdnDtl,gdnDtlGoods,goodsVersion,nowDate);
                            gdnDtlGoodsMapper.insert(gdnDtlGoods);
                            TrsnDtlGoods trsnDtlGoods=new TrsnDtlGoods();
                            trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
                            trsnDtlGoods.setGoodsSn(goodsVersion.getGoodsSn());
                            trsnDtlGoods.setGoodsId(goodsVersion.getGoodsId());
                            trsnDtlGoods.setQty(goodsVersion.getQty());
                            trsnDtlGoods.setQaStatus(goodsVersion.getQaStatus());
                            trsnDtlGoods.setComments("["+oqcInsp.getInspNo()+"]判检出库");
                            trsnDtlGoods.setStatus(goodsVersion.getStatus());
                            trsnDtlGoods.setCreateUser("oqc-system");
                            trsnDtlGoods.setCreateTime(nowDate);
                            trsnDtlGoods.setModifyUser("oqc-system");
                            trsnDtlGoods.setModifyTime(nowDate);
                            trsnDtlGoodsMapper.insert(trsnDtlGoods);
                        }
                        gdnLine++;
                    }
					/*for (GoodsVersion goodsVersion : headerGoodsList) {
						TrsnHeader trsnHeader=new TrsnHeader();
						trsnHeader.setNoteNo(basicSimpleCodeRuleApi.GenerateCode(TRSN_ORDER));
						trsnHeader.setNoteType(TrsnNoteTypeEnum.OUT_STORAGE.getCode());
						trsnHeader.setOwner(goodsVersion.getOwner());
						trsnHeader.setFactory(goodsVersion.getFactory());
						trsnHeader.setWhsId(goodsVersion.getWhsId());
						trsnHeader.setVendorId(goodsVersion.getVendorId());
						trsnHeader.setFmDoc(gdnHeader.getGdnNo());
						trsnHeader.setCreateUser("oqc-system");
						trsnHeader.setStatus(TrsnHeaderStatusEnum.CMP.code);
						trsnHeader.setCreateTime(nowDate);
						trsnHeaderMapper.insert(trsnHeader);

						TrsnDtl trsnDtl=new TrsnDtl();
						trsnDtl.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
						trsnDtl.setNoteNo(trsnHeader.getNoteNo());
						trsnDtl.setNoteLineNo("1");
						trsnDtl.setFmDoc(trsnHeader.getFmDoc());
						trsnDtl.setFmDocLine("/");
						trsnDtl.setInvUpdateFlag(TrsnDtlInvUpdateFlagEnum.OUT.code);
						trsnDtl.setSkuId(goodsVersion.getSkuId());
						trsnDtl.setQty(goodsVersion.getQty());
						trsnDtl.setUnit(goodsVersion.getUnit());
						trsnDtl.setInvAttr(goodsVersion.getInvAttr());
						trsnDtl.setQaStatus(goodsVersion.getQaStatus());
						trsnDtl.setLotAttr1(goodsVersion.getPoNo());
						trsnDtl.setLotAttr2(goodsVersion.getSoNo());
						trsnDtl.setLotAttr3(goodsVersion.getSoItem());
						trsnDtl.setStatus(TrsnDtlStatusEnum.CMP.code);
						trsnDtl.setCreateUser("oqc-system");
						trsnDtl.setCreateTime(nowDate);
						trsnDtlMapper.insert(trsnDtl);

						TrsnDtlGoods trsnDtlGoods=new TrsnDtlGoods();
						trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
						trsnDtlGoods.setGoodsSn(goodsVersion.getGoodsSn());
						trsnDtlGoods.setGoodsId(goodsVersion.getGoodsId());
						trsnDtlGoods.setQty(goodsVersion.getQty());
						trsnDtlGoods.setQaStatus(goodsVersion.getQaStatus());
						trsnDtlGoods.setComments("["+oqcInsp.getInspNo()+"]判检出库");
						trsnDtlGoods.setStatus(goodsVersion.getStatus());
						trsnDtlGoods.setCreateUser("oqc-system");
						trsnDtlGoods.setCreateTime(nowDate);
						trsnDtlGoods.setModifyUser("oqc-system");
						trsnDtlGoods.setModifyTime(nowDate);
						trsnDtlGoodsMapper.insert(trsnDtlGoods);
					}*/
                }
                log.info(entry.getKey()+"生成出库单结束");

                //扣减库存
                log.info(entry.getKey()+"扣减库存开始");
                invGoodsCollect=allGoodsList.stream().collect(
                        Collectors.groupingBy(t -> t.getWhsId() + "#" +
                                t.getLocId() + "#" +t.getOwner() + "#" +
                                t.getSkuId() + "#" +t.getFactory() + "#" +
                                t.getVendorId() + "#" +t.getLotno()+ "#" +t.getInvAttr(), Collectors.toList()));

                log.info(entry.getKey()+"扣减库存开始");

                for (Map.Entry<String, List<GoodsVersion>> invEntry : invGoodsCollect.entrySet()) {
                    UpdateInventoryInputDto inventoryInputDto;
                    inventoryInputDto = this.setInventoryInputDto(invEntry.getValue(), "oqc-system");
                    try {
                        inventoryService.updateLocInventroy(inventoryInputDto);
                        inventoryService.updateZoneInventory(inventoryInputDto);
                    }catch (Exception e){
                        throw new Exception(e.toString()+"当前主档分组判检成功的数量为0");
                    }
                }

/*				//生成出库凭证
				trsnHeaderCollect = allGoodsList.stream().collect(
						Collectors.groupingBy(t -> t.getOwner() + "#" +
								t.getFactory() + "#" + t.getVendorId() + "#" + t.getWhsId(), Collectors.toList()));
				for (Map.Entry<String, List<GoodsVersion>> trsnHeaderEntry : trsnHeaderCollect.entrySet()) {
					int count=1;
					trsnHeaderList=trsnHeaderEntry.getValue();
					TrsnHeader trsnHeader=new TrsnHeader();
					trsnHeader.setNoteNo(basicSimpleCodeRuleApi.GenerateCode(TRSN_ORDER));
					trsnHeader.setNoteType(TrsnNoteTypeEnum.OUT_STORAGE.getCode());
					trsnHeader.setOwner(trsnHeaderList.get(0).getOwner());
					trsnHeader.setFactory(trsnHeaderList.get(0).getFactory());
					trsnHeader.setWhsId(trsnHeaderList.get(0).getWhsId());
					trsnHeader.setVendorId(trsnHeaderList.get(0).getVendorId());
					trsnHeader.setFmDoc(oqcInsp.getInspNo());
					trsnHeader.setCreateUser("oqc-system");
					trsnHeader.setStatus(TrsnHeaderStatusEnum.CMP.code);
					trsnHeader.setCreateTime(nowDate);
					trsnHeaderMapper.insert(trsnHeader);
					trsnDtlCollect=trsnHeaderList.stream().collect(
							Collectors.groupingBy(t -> t.getLotno() + "#" +
									t.getQaStatus()+ "#" +t.getSkuId(), Collectors.toList()));
					for (Map.Entry<String, List<GoodsVersion>> trsnDtlEntry : trsnDtlCollect.entrySet()) {
						trsnDtlList=trsnHeaderEntry.getValue();
						trsnQty=trsnDtlList.stream()
								.map(GoodsVersion::getQty)
								.reduce(BigDecimal.ZERO,BigDecimal::add);
						TrsnDtl trsnDtl=new TrsnDtl();
						trsnDtl.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
						trsnDtl.setNoteNo(trsnHeader.getNoteNo());
						trsnDtl.setNoteLineNo(""+count);
						trsnDtl.setFmDoc(oqcInsp.getInspNo());
						trsnDtl.setFmDocLine("/");
						trsnDtl.setInvUpdateFlag(TrsnDtlInvUpdateFlagEnum.OUT.code);
						trsnDtl.setSkuId(trsnDtlList.get(0).getSkuId());
						trsnDtl.setQty(trsnQty);
						trsnDtl.setUnit(trsnDtlList.get(0).getUnit());
						trsnDtl.setInvAttr(trsnDtlList.get(0).getInvAttr());
						trsnDtl.setQaStatus(trsnDtlList.get(0).getQaStatus());
						trsnDtl.setLotAttr1(trsnDtlList.get(0).getPoNo());
						trsnDtl.setLotAttr2(trsnDtlList.get(0).getSoNo());
						trsnDtl.setLotAttr3(trsnDtlList.get(0).getSoItem());
						trsnDtl.setStatus(TrsnDtlStatusEnum.CMP.code);
						trsnDtl.setCreateUser("oqc-system");
						trsnDtl.setCreateTime(nowDate);
						trsnDtlMapper.insert(trsnDtl);
						count++;
						for (GoodsVersion goodsVersion : trsnDtlList) {
							TrsnDtlGoods trsnDtlGoods=new TrsnDtlGoods();
							trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
							trsnDtlGoods.setGoodsSn(goodsVersion.getGoodsSn());
							trsnDtlGoods.setGoodsId(goodsVersion.getGoodsId());
							trsnDtlGoods.setQty(goodsVersion.getQty());
							trsnDtlGoods.setQaStatus(goodsVersion.getQaStatus());
							trsnDtlGoods.setComments("["+oqcInsp.getInspNo()+"]判检出库");
							trsnDtlGoods.setStatus(goodsVersion.getStatus());
							trsnDtlGoods.setCreateUser("oqc-system");
							trsnDtlGoods.setCreateTime(nowDate);
							trsnDtlGoods.setModifyUser("oqc-system");
							trsnDtlGoods.setModifyTime(nowDate);
							trsnDtlGoodsMapper.insert(trsnDtlGoods);
						}
					}
				}*/


                //记录日志，且删除SN，移入HIS表
                GoodsLog goodsLog=new GoodsLog();
                GoodsHis goodsHis=new GoodsHis();
                for (GoodsVersion goods : allGoodsList) {
                    //写SN操作日志
                    goodsLog= this.setGoodsLog(oqcInsp.getInspNo(), "oqc-system", nowDate, goods);
                    goodsLogMapper.insert(goodsLog);
                    //添加SN到HIS表
                    BeanUtil.copyNonNull(goods, goodsHis);
                    goodsHis.setLogCreateUser("oqc-system");
                    goodsHis.setLogCreateTime(nowDate);
                    goodsHis.setStatus(GoodsStatusEnum.HAS_CHUKU.code);
                    goodsHis.setWhsId(null);
                    goodsHis.setLocId(null);
                    goodsHis.setCreateUser("oqc-system");
                    goodsHis.setCreateTime(new Date());
                    goodsHis.setModifyTime(new Date());
                    goodsHis.setModifyUser("oqc-system");
                    goodsHisMapper.insert(goodsHis);
                }
                //删除SN
                List<String> deleteSnList = allGoodsList.stream().map(t -> t.getGoodsSn()).collect(Collectors.toList());
                goodsMapper.delete(new QueryWrapper<Goods>().in("GOODS_SN",deleteSnList));
            }

        }
    }

    private void setGdnHeader(GdnHeader gdnHeader, Integer owner, String dataSource, Date nowDate,String factory){
        gdnHeader.setGdnNo(this.basicSimpleCodeRuleApi.GenerateCode(GDN_D98));  //单号
        gdnHeader.setSourceShipNotice(dataSource);
        gdnHeader.setGdnType(GdnTypeEnum.D98.code);
        gdnHeader.setOwner(owner);
        gdnHeader.setFactory(factory);
        gdnHeader.setShipDate(nowDate);
        gdnHeader.setCloseEndTime(nowDate);
        gdnHeader.setDataSource(dataSource);
        gdnHeader.setStatus(GdnHeaderStatusEnum.FINISH.code);
        gdnHeader.setComments(dataSource+"判检出库");
        gdnHeader.setCreateUser("oqc-system");
        gdnHeader.setGdnCreateTime(nowDate);
        gdnHeader.setModifyTime(nowDate);
        gdnHeader.setModifyUser("oqc-system");
        gdnHeader.setCreateTime(nowDate);
    }

    private void setGdnDtl(GdnHeader gdnHeader,GdnDtl gdnDtl, Integer count,
                           GoodsVersion goods, Date nowDate,BigDecimal dtlQty){
        gdnDtl.setGdnHeaderId(gdnHeader.getGdnHeaderId());
        gdnDtl.setGdnLineNo(count);
        gdnDtl.setWhsId(goods.getWhsId());
        gdnDtl.setSkuId(goods.getSkuId());
        gdnDtl.setPoNo(goods.getPoNo());
        gdnDtl.setSoNo(goods.getSoNo());
        gdnDtl.setSoLineNo(goods.getSoItem());
        gdnDtl.setRefSoNo(goods.getSoNo());
        gdnDtl.setRefSoLineNo(goods.getSoItem());
        gdnDtl.setOrderQty(dtlQty);
        gdnDtl.setPickQty(dtlQty);
        gdnDtl.setShipQty(dtlQty);
        gdnDtl.setUnit(goods.getUnit());
        gdnDtl.setInvAttr(goods.getInvAttr());
        gdnDtl.setQaStatus(goods.getQaStatus());
        gdnDtl.setLotno(goods.getLotno());
        gdnDtl.setComments(gdnHeader.getComments());
        gdnDtl.setStatus(GdnDtlStatusEnum.SHIPMENTED.code);
        gdnDtl.setCreateUser("oqc-system");
        gdnDtl.setCreateTime(nowDate);
    }

    private void setGdnDtlGoods(GdnDtl gdnDtl,GdnDtlGoods gdnDtlGoods,GoodsVersion goods,Date nowDate){
        gdnDtlGoods.setGdnDtlId(gdnDtl.getGdnDtlId());
        gdnDtlGoods.setGoodsSn(goods.getGoodsSn());
        gdnDtlGoods.setGoodsId(goods.getGoodsId());
        gdnDtlGoods.setQty(goods.getQty());
        gdnDtlGoods.setQaStatus(goods.getQaStatus());
        gdnDtlGoods.setStatus(GdnDtlGoodsStatusEnum.OS_SHIP.code);
        gdnDtlGoods.setCreateUser("oqc-system");
        gdnDtlGoods.setCreateTime(nowDate);
    }

    //生成库存信息
    private UpdateInventoryInputDto setInventoryInputDto(
            List<GoodsVersion> goodsList,String loginName){
        UpdateInventoryInputDto inventoryInputDto = new UpdateInventoryInputDto();
        BigDecimal qty = goodsList.stream().map(GoodsVersion::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
        inventoryInputDto.setWhsId(goodsList.get(0).getWhsId());
        inventoryInputDto.setLocId(goodsList.get(0).getLocId());
        inventoryInputDto.setOwner(goodsList.get(0).getOwner());
        inventoryInputDto.setSkuId(goodsList.get(0).getSkuId());
        inventoryInputDto.setFactory(goodsList.get(0).getFactory());
        inventoryInputDto.setVendorId(goodsList.get(0).getVendorId());
        inventoryInputDto.setLotNo(goodsList.get(0).getLotno());
        inventoryInputDto.setInvAttr(goodsList.get(0).getInvAttr());
        inventoryInputDto.setQaStatus(goodsList.get(0).getQaStatus());
        inventoryInputDto.setOperator(loginName);
        inventoryInputDto.setQty(qty.negate());
        //注意翻牌出库没有拣料，无需扣除分配数【拣料会加分配数】
//		inventoryInputDto.setAlocQty(qty.negate());
        inventoryInputDto.setUnit(goodsList.get(0).getUnit());
        return inventoryInputDto;
    }

    //生成操作日志
    private GoodsLog setGoodsLog(String inspNo,String loginName,Date logDate,GoodsVersion goods){
        GoodsLog goodsLog = new GoodsLog();
        goodsLog.setGoodsSn(goods.getGoodsSn());
        goodsLog.setOpCode(OpCodeEnum.SHIPPED.getCode());
        goodsLog.setReason("出库确认，判检单号["+inspNo+"]");
        String goodsJson="未更新的:" + JSON.toJSONString(goods)+ ",更新的:{条码"+goods.getGoodsSn()+"删除移动到HIS表}";
        goodsLog.setUpdateContent(goodsJson);
        goodsLog.setOperator(loginName);
        goodsLog.setCreateUser(loginName);
        goodsLog.setCreateTime(logDate);
        goodsLog.setModifyUser(loginName);
        goodsLog.setModifyTime(logDate);
        return goodsLog;
    }
}
